package org.nidy.programexercise.IOExercise;

import java.io.*;

public class ByteStream {
    public static void main(String[] args) throws IOException {
        File file=new File("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\a.txt");
        FileOutputStream fos = new FileOutputStream(file, true);
        //单字节写入
        fos.write(97);
        byte[] bytes = {97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122};
        //换行
        fos.write("\r\n".getBytes());
        //多字节写入
        fos.write(bytes);
        //部分字节写入
        fos.write("\r\n".getBytes());
        fos.write(bytes,0,3);
        fos.close();
        //读取
        FileInputStream fis = new FileInputStream(file);
        int read;
        while ((read=fis.read()) != -1) {
            System.out.print((char) read);
        }
        fis.close();

        //拷贝文件
        FileInputStream fin = new FileInputStream(file);
        FileOutputStream fout = new FileOutputStream("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\b.txt");
        int fLen;
        byte[] fByte=new byte[1024*1024*2];
        while ((fLen=fin.read(fByte)) != -1) {
            fout.write(fByte,0,fLen);
        }
        fout.close();
        fin.close();

        System.out.println("==================字节缓冲流==========================");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\main\\java\\org\\nidy\\programexercise\\IOExercise\\c.txt"));
        byte[] bs = new byte[1024*1024*2];
        int len;
        while((len=bis.read(bs))!=-1){
            bos.write(bs,0,len);
        }
        bos.close();
        bis.close();
    }
}
